package com.marvels.financial.collection.service.impl;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.dao.McfeCollecteConfigurationDao;
import com.marvels.financial.collection.dto.IntelligentCollectDTO;
import com.marvels.financial.collection.dto.McfeCollecteConfigurationDTO;
import com.marvels.financial.collection.service.IntelligentCollectApi;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 智能催收实现
 * @author tangpw
 *
 */
@Service
public class IntelligentCollectImpl implements IntelligentCollectApi{

	@Autowired
	private McfeCollecteConfigurationDao configurationDao;
	@Override
	public void addCollectionConfig(IntelligentCollectDTO collectDTO) throws MarvelsBusinessException{
		McfeCollecteConfigurationDTO dto=new McfeCollecteConfigurationDTO();
		JSONObject zc360Json=new JSONObject();
		zc360Json.put("orgId", "001");//资产360
		zc360Json.put("orgName", "资产360");//资产360
		zc360Json.put("yyzc360", collectDTO.getYyzc360Radio());
		zc360Json.put("rgzc360", collectDTO.getRgzc360Radio());
		
		JSONObject zytJson=new JSONObject();
		zytJson.put("orgId", "002");//资易通
		zytJson.put("orgName", "资易通");//资易通
		zytJson.put("yyzyt", collectDTO.getYyzytRadio());
		zytJson.put("rgzyt", collectDTO.getRgzytRadio());
		
		
		JSONObject yfwJson=new JSONObject();
		yfwJson.put("orgId", "003");//云孚微
		yfwJson.put("orgName", "云孚微");//云孚微
		yfwJson.put("yyyfw", collectDTO.getYyyfwRadio());
		yfwJson.put("rgyfw", collectDTO.getRgyfRadio());
		JSONArray array=new JSONArray();
		array.add(zc360Json);
		array.add(zytJson);
		array.add(yfwJson);
		dto.setPercent(array.toString());
		StringBuilder fundCode=new StringBuilder();
		StringBuilder fundName=new StringBuilder();
		if(null!=collectDTO.getFundGroup() && collectDTO.getFundGroup().size()>0){
			for(int i=0;i<collectDTO.getFundGroup().size();i++){
				String fund=collectDTO.getFundGroup().get(i);
				if(i==collectDTO.getFundGroup().size()-1){
					fundCode.append(fund.split(",")[0]);
					fundName.append(fund.split(",")[1]);
				}else{
					fundCode.append(fund.split(",")[0]).append(",");
					fundName.append(fund.split(",")[1]).append(",");
				}
			}
		}
		StringBuilder productCode=new StringBuilder();
		StringBuilder productName=new StringBuilder();
		if(null!=collectDTO.getProductGroup() && collectDTO.getProductGroup().size()>0){
			for(int i=0;i<collectDTO.getProductGroup().size();i++){
				String product=collectDTO.getProductGroup().get(i);
				if(i==collectDTO.getProductGroup().size()-1){
					productCode.append(product.split(",")[0]);
					productName.append(product.split(",")[1]);
				}else{
					productCode.append(product.split(",")[0]).append(",");
					productName.append(product.split(",")[1]).append(",");
				}
			}
		}
		StringBuilder channelCode=new StringBuilder();
		StringBuilder channelName=new StringBuilder();
		if(null!=collectDTO.getChannelGroup() && collectDTO.getChannelGroup().size()>0){
			for(int i=0;i<collectDTO.getChannelGroup().size();i++){
				String channel=collectDTO.getChannelGroup().get(i);
				if(i==collectDTO.getChannelGroup().size()-1){
					channelCode.append(channel.split(",")[0]);
					channelName.append(channel.split(",")[1]);
				}else{
					channelCode.append(channel.split(",")[0]).append(",");
					channelName.append(channel.split(",")[1]).append(",");
				}
			}
		}
		dto.setProductId(productCode.toString());
		dto.setProductName(productName.toString());
		dto.setChannelId(channelCode.toString());
		dto.setChannelName(channelName.toString());
		dto.setFundCode(fundCode.toString());
		dto.setFundName(fundName.toString());
		UserDTO user = CacheUtils.getCacheUser();
		dto.setCreatedBy(user.getUserCode());
		dto.setCreatedDate(new Date());
		dto.setUpdatedBy(user.getUserCode());
		dto.setUpdatedDate(new Date());
		int count=configurationDao.insertSelective(dto);
		if(count<1){
			throw new MarvelsBusinessException("添加异常");
		}
		
	}

	
	
}
